

//System Library
#include "wblib.h"
#include "NUC900_reg.h"
//NON OS Library
#include "NUC900_kpi.h"

//defin by project
#define __WB_EVB__
//#define __WB_POSTAX__

#ifdef __WB_EVB__
#define ESC_ROW    0
#define ESC_COL    1
#endif

#ifdef __WB_ICV_PORT6__
#define ESC_ROW    0
#define ESC_COL    0
#endif

#ifdef __WB_ICV_PORT25__
#define ESC_ROW    0
#define ESC_COL    0
#endif

#ifdef __WB_POSTAX__
#define ESC_ROW    7
#define ESC_COL    0
#endif

//Function Definition
VOID KPI_Test(VOID);
static VOID KPI_Item(VOID);
static VOID KPI_Readkey_block(VOID);
static VOID KPI_Readkey_nonblock(VOID);
static VOID KPI_BufferTest(VOID);
static VOID KPI_ChangeKPICONF(VOID);
static VOID KPI_LPWakeTest(VOID);
static VOID KPI_ThreeKeyTest(VOID);
static VOID KPI_ShowKeyMap(VOID);

//#define sysprintf sysprintf 




/****************************************************************************
* FUNCTION
*   KPI_Test
*
* DESCRIPTION
*   The KPI test main function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
INT diag_kpi(VOID)
{
	//UINT32 tmp32;
   
  	WB_UART_T	my_uart;    
  	
  	CHAR cUserSelect;
  
                                            
    /* UART setting */

	my_uart.uiFreq     = 15000000; /* 15MHZ */
	my_uart.uiBaudrate = 115200;
	my_uart.uiDataBits = WB_DATA_BITS_8;
	my_uart.uiStopBits = WB_STOP_BITS_1;
	my_uart.uiParity   = WB_PARITY_NONE;
	my_uart.uiRxTriggerLevel = LEVEL_1_BYTE;
	
	sysInitializeUART(&my_uart);

	KPI_Init();
    do
    {
        KPI_Item();// Show menu
        
        cUserSelect = sysGetChar();// Get user select
        
        switch(cUserSelect)
        {
            case '1':   KPI_LPWakeTest();           break;
            case '2':   KPI_Readkey_block();        break;
            case '3':   KPI_Readkey_nonblock();     break;
            case '4':   KPI_BufferTest();           break;
            case '5':   KPI_ThreeKeyTest();         break;
           // case '6':   KPI_ChangeKPICONF();        break;
            default:    break;
        }
    }while(cUserSelect != 27);// Do until user press ESC
    
 
    return 0;
}

/****************************************************************************
* FUNCTION
*   KPI_Item
*
* DESCRIPTION
*   The KPI test program menu
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_Item(VOID)
{
    sysprintf("\n\n\n");
    sysprintf("+------------------------------------------------------+\n");
    sysprintf("| Nuvoton KPI Diagnostic Code                          |\n");
    sysprintf("+------------------------------------------------------+\n");
    sysprintf("| Power down wake up test                        - [1] |\n");
    sysprintf("| KPI block mode test                            - [2] |\n");
    sysprintf("| KPI non-block mode test                        - [3] |\n");
    sysprintf("| KPI buffer test                                - [4] |\n");
    sysprintf("| KPI three key test                             - [5] |\n");
   // sysprintf("| Change KPI_CONF example code                   - [6] |\n");
    sysprintf("| Quit                                         - [ESC] |\n");
    sysprintf("+------------------------------------------------------+\n");
    sysprintf("Please Select : \n");
}

/****************************************************************************
* FUNCTION
*   KPI_ThreeKeyTest
*
* DESCRIPTION
*   The KPI three press test function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_ThreeKeyTest(VOID)
{
    INT nStatus;
    CHAR cItem;
    KPISTATUS_union kpikey;
    KPI3KCONF_union kpi3kconf;
    kpikey.value = 0;

    KPI_Open(PORTI);
    KPI_ShowKeyMap();
    sysprintf("Setup KPI3KCONF-ENRST\nReset?(y/n)");
    cItem = sysGetChar();// Get user key
    switch(cItem)
    {
        case 'y':    ENRST = ENRST_ENABLE;    break;
        case 'Y':    ENRST = ENRST_ENABLE;    break;
        case 'n':    ENRST = ENRST_DISABLE;   break;
        case 'N':    ENRST = ENRST_DISABLE;   break;
        default:     ENRST = ENRST_DISABLE;   break;
    }

    #ifdef __WB_EVB__
    K30R = 1;
    K30C = 0;
    K31R = 1;
    K31C = 1;
    K32R = 1;
    K32C = 2;
    #endif

    #ifdef __WB_POSTAX__
    K30R = 3;
    K30C = 0;
    K31R = 4;
    K31C = 1;
    K32R = 5;
    K32C = 2;
    #endif

    #ifdef __WB_ICV_PORT25__
    K30R = 0;
    K30C = 0;
    K31R = 1;
    K31C = 1;
    K32R = 2;
    K32C = 2;
    #endif

    #ifdef __WB_ICV_PORT6__
    K30R = 0;
    K30C = 0;
    K31R = 1;
    K31C = 1;
    K32R = 2;
    K32C = 2;
    #endif

    EN3KY = EN3KY_ENABLE;

    nStatus = KPI_Ioctl(SET_KPI3KCONF, 0, kpi3kconf.value);
    if(nStatus == kpiInvalidIoctlCommand)
    {
        sysprintf("KPI ioctl command error!");
        return;
    }
    sysprintf("keypad three key test.\n");
    sysprintf("Three key setup:\n  Key1 R:%2d C:%2d\n  Key2 R:%2d C:%2d\n  Key3 R:%2d C:%2d\n",
               K30R, K30C, K31R, K31C, K32R, K32C);
    if( (cItem == 'y') || (cItem == 'Y') )
    {
        sysprintf("  Reset: yes\n");
    }
    else
    {
        sysprintf("  Reset: no\n");
    }
    sysprintf("Wait for three key press...(ESC: ROW: %d, COL: %d)\n", ESC_ROW, ESC_COL);
    while(1)
    {
        KPI_Read((PUCHAR)&kpikey, BLOCK_MODE);
        if(_1KEY)
        {
            if(KEY0R == 0 && KEY0C == 1)
            {
                break;
            }
        }
        if(_3KEY)
        {
            sysprintf("Three key test OK!!\n\n");
            sysprintf("Wait for three key press...(ESC: ROW: %d,COL: %d)\n", ESC_ROW, ESC_COL);
        }
    }
    KPI_Close();
    sysprintf("\n\nTest finish\n");
}

/****************************************************************************
* FUNCTION
*   KPI_LPWakeTest
*
* DESCRIPTION
*   The KPI low power wake up test function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_LPWakeTest(VOID)
{
    INT i ;
    //UINT32 tmp32;
    INT nStatus;
    KPISTATUS_union kpikey;
    KPILPCONF_union kpilpconf;
    
    kpikey.value = 0;
    KPI_Open(PORTI);
   
    //KPI_ShowKeyMap();
    //sysInvalidCache();
    //sysEnableCache(CACHE_WRITE_BACK);
    //sysFlushCache(I_D_CACHE);
 
    LPWAKE = WAKE_ENABLE;
 
    LPWR = LPWR_ROW1;

    LPWCEN = LPWCEN_COL0;

    nStatus = KPI_Ioctl(SET_KPILPCONF, 0, kpilpconf.value);
   
    if(nStatus == kpiInvalidIoctlCommand)
    {
        sysprintf("KPI ioctl command error!");
        return;
    }

    sysprintf("KPI power down wake up test\n");

    #ifdef __WB_EVB__
    sysprintf("KPI ROW:0 COL:0 for wake up\n");
    #endif

    #ifdef __WB_POSTAX__
    sysprintf("KPI ROW:0 COL:ANY for wake up\n");
    #endif

    sysprintf("Press any key to enter power down mode....");
    
    /* Wait until the transmitter buffer is empty */
	//while( !(inpb(REG_UART0_LSR) & 0x20) );  
    
    sysGetChar();
    KPI_Ioctl(CLEAN_KPI_BUFFER, 0, 0);
    sysprintf("\n");
    for(i=0;i<0x200000;i++);
    
    writew(0xb0000214,0x2); // clock stop (PD)
    
    //for(i=0;i<0xA00000;i++);


    //Notice:
    //   Since the KPI interrupt happens twice after system wake up, application
    //   should read key twice in order to get correct key which wake up system.
    //   The first interrupt do not record any correct key information in KPISTATUS.
	
    KPI_Read((PUCHAR)&kpikey, BLOCK_MODE);
    
    if(PDWAKE==1)
    {
        sysprintf("Wake up success.\n");
        KPI_Read((PUCHAR)&kpikey, BLOCK_MODE);
        if(_1KEY)
        {
            sysprintf("\nKey1 ROW:%2d, COL:%2d\n", KEY0R, KEY0C);
        }
    }
    KPI_Close();
    sysprintf("\n Power down Test finish!\nPress any key to continue....");
    sysGetChar();
}

/****************************************************************************
* FUNCTION
*   KPI_Readkey_block
*
* DESCRIPTION
*   The KPI block mode read key test function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_Readkey_block(VOID)
{
    INT nStatus;
    KPISTATUS_union kpikey;
    kpikey.value = 0;
    KPI_Open(PORTI);
    KPI_ShowKeyMap();
    sysprintf("KPI block mode test\n");
    sysprintf("1Key [1, 4] support continuous mode\n");
    sysprintf("2Key [1, 4],[1, 5]  support continuous mode\n");
    sysprintf("Please press any KPI key\n(ESC: ROW: %d, COL: %d)\n", ESC_ROW, ESC_COL);
    while(1)
    {
        nStatus = KPI_Read((PUCHAR)&kpikey, BLOCK_MODE);
        if(nStatus != Successful)
        {
            sysprintf("KPI read function error!\nError code:%x\n", nStatus);
            return;
        }
        if(_1KEY)
        {
            sysprintf("Key1 ROW:%2d, COL:%2d\n", KEY0R, KEY0C);
            if(KEY0R == ESC_ROW && KEY0C == ESC_COL)
            {
                break;
            }
            else if(KEY0R == 1 && KEY0C == 4)
            {
            	KPI_Ioctl(CONTINUOUS_MODE, 0, 1);
            }
        }
        else if(_2KEY)
        {
            sysprintf("Key1 ROW:%2d, COL:%2d    Key2 ROW:%2d, COL:%2d\n", KEY0R, KEY0C, KEY1R, KEY1C);
            if(KEY0R == 1 && KEY0C == 4 && KEY1R == 1 && KEY1C == 5)
            {
	            KPI_Ioctl(CONTINUOUS_MODE, 0, 1);
            }
        }
    }
    KPI_Close();
    sysprintf("\n\nKPI block mode test finish!\n");
}

/****************************************************************************
* FUNCTION
*   KPI_Readkey_nonblock
*
* DESCRIPTION
*   The KPI non-block mode read key test function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_Readkey_nonblock(VOID)
{
    INT nStatus;
    KPISTATUS_union kpikey;
    INT nLoop;
    kpikey.value = 0;

    KPI_Open(PORTI);
    KPI_ShowKeyMap();
    sysprintf("KPI non block mode test\n");
    sysprintf("Please press any KPI key before time out\n");
    for(nLoop = 0; nLoop < 2000000; nLoop++)
    {
        if(nLoop%100000 == 0)
        {
            sysprintf(".");
        }
        nStatus = KPI_Read((PUCHAR)&kpikey, NONBLOCK_MODE);
        if(nStatus == kpiReadModeError)
        {
            sysprintf("KPI read function read mode error!");
            return;
        }
        if(nStatus == kpiNoKey)
        {
            continue;
        }
        if(_1KEY)
        {
            sysprintf("\nKey1 ROW:%2d, COL:%2d\n", KEY0R, KEY0C);
            break;
        }
    }
    KPI_Close();
    sysprintf("\n\nKPI non block mode test finish!\nPress any key to continue....");
    sysGetChar();
}

/****************************************************************************
* FUNCTION
*   KPI_BufferTest
*
* DESCRIPTION
*   The KPI key buffer test function
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_BufferTest(VOID)
{
    KPISTATUS_union kpikey;
    INT nBuffercount = 0;

    KPI_Open(PORTI);
    KPI_ShowKeyMap();
    sysprintf("\nKPI buffer test\nThe KPI buffer size is ten\nPlease press any KPI key then press any key in PC\n");
    sysGetChar();
    while(KPI_Read((PUCHAR)&kpikey, NONBLOCK_MODE) != kpiNoKey)
    {
        if(_1KEY)
        {
            nBuffercount++;
            sysprintf("[Buffer %2d]Key1 ROW:%2d, COL:%2d\n", nBuffercount, KEY0R, KEY0C);
        }
        else if(_2KEY)
        {
            nBuffercount++;
            sysprintf("[Buffer %2d]Key1 ROW:%2d, COL:%2d    Key2 ROW:%2d, COL:%2d\n", nBuffercount, KEY0R, KEY0C, KEY1R, KEY1C);
        }
    }
    KPI_Close();
    sysprintf("KPI buffer test finish!\nPress any key to continue....");
    sysGetChar();
}


#if 0
/****************************************************************************
* FUNCTION
*   KPI_ChangeKPICONF
*
* DESCRIPTION
*   The KPI change configure example code
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/
static VOID KPI_ChangeKPICONF(VOID)
{
    INT nStatus;
    KPICONF_union kpiconf;
    //CHAR cItem;
#if 0 
    #ifdef __WB_EVB__
    ENCODE=ENCODE_DISABLE;
    KPSEL=KPSEL_PORT25;
    KSIZE=KSIZE_00;
    #endif

    #ifdef __WB_POSTAX__
    ENCODE=ENCODE_ENABLE;
    KPSEL=KPSEL_PORT6;
    KSIZE=KSIZE_01;
    #endif

    #ifdef __WB_ICV_PORT25__
    ENCODE=ENCODE_DISABLE;
    KPSEL=KPSEL_PORT25;
    KSIZE=KSIZE_1X;
    #endif

    #ifdef __WB_ICV_PORT6__
    ENCODE=ENCODE_DISABLE;
    KPSEL=KPSEL_PORT6;
    KSIZE=KSIZE_1X;
    #endif
#endif
    PRESCALE = 0xFA;
    DBTC = 0x5;
    ENKP = ENKP_ENABLE;
    ODEN = ODEN_DISABLE;
   
   
    sysprintf("===   Current Setting   === \n");
    sysprintf("ENCODE\t\t=0x%x\n", ENCODE);
    sysprintf("ODEN\t\t=0x%x\n",   ODEN);
    sysprintf("KPSEL\t\t=0x%x\n",  KPSEL);
    sysprintf("ENKP\t\t=0x%x\n",   ENKP);
    sysprintf("KSIZE\t\t=0x%x\n",  KSIZE);
    sysprintf("PRESCALE\t=0x%x\n", PRESCALE);
    sysprintf("DBTC\t\t=0x%x\n",   DBTC);
    
    
    //Set KPI_CONF
    sysprintf("\nKPI_KCONF: %08x\n", kpiconf.value);
    nStatus = KPI_Ioctl(SET_KPICONF, 0, kpiconf.value);
    if(nStatus == kpiInvalidIoctlCommand)
    {
        sysprintf("KPI ioctl command error!");
        return;
    }
    sysprintf("Press any key to continue....");
    sysGetChar();
}
#endif


/****************************************************************************
* FUNCTION
*   KPI_ShowKeyMap
*
* DESCRIPTION
*   This function the keypad key may of evaluation board/PSO-TAX demo board
*
* CALLED BY
*   various components
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*   NS21 WTLiu	2005-12-21	Created initial version
*
* REMARK
*     None
****************************************************************************/



static VOID KPI_ShowKeyMap(VOID)
{
    sysprintf("Key may:[ROM,COL]\n");
	sysprintf("[0,0][0,1][0,2][0,3] [2,0][2,1][2,2][2,3]\n");
	sysprintf("[1,0][1,1][1,2][1,3] [3,0][3,1][3,2][3,3]\n");

}
#if 0 

#ifdef __WB_EVB__
static VOID KPI_ShowKeyMap(VOID)
{
    sysprintf("Key may:[ROM,COL]\n");
	sysprintf("[0,0][0,1][0,2][0,3]\n");
	sysprintf("[1,0][1,1][1,2][1,3]\n");
	sysprintf("[0,4][0,5][0,6][0,7]\n");
	sysprintf("[1,4][1,5][1,6][1,7]\n");
}
#endif

#ifdef __WB_POSTAX__
VOID KPI_ShowKeyMap(VOID)
{
    sysprintf("\nKey may:[ROM/COL]\n");
	sysprintf("[70][71][72] [73][74][75][76][77][67][06][05][04][03]\n");
	sysprintf("[60][61][62] [63][64][65][66][67][17][16][15][14][13]\n");
	sysprintf("[50][51][52] [53][54][55][56][57][27][26][25][24][23]\n");
	sysprintf("[40][41][42] [43][44][45][46][47][12][02][01][  20  ]\n");
    sysprintf("[30][31][32] [33][34][35][36][37][22][21][11][  10  ]\n\n");
}
#endif

#ifdef __WB_ICV_PORT6__
static VOID KPI_ShowKeyMap(VOID)
{
    return;
}
#endif

#ifdef __WB_ICV_PORT25__
static VOID KPI_ShowKeyMap(VOID)
{
    return;
}
#endif


#endif

